//bench.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({
  data: {
    imgArray: []
  },
  onLoad: function (options) {
    let that = this
    let location = options.location || '未知'
    that.setData({
      pileCode: options.pileCode,
      hospitalName: options.hospitalName,
      hospitalId: options.hospitalId,
      location: options.location,
      scrollHeight:app.getScrollHeight(0)
    })
  },
  onReady: function () {
    // Do something when page ready.
  },
  onShow: function () {
    // Do something when page show.
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  onPullDownRefresh: function () {
    // Do something when pull down
  },
  // Event handler.
  addImg: function (e) {
    let that = this;
    wx.chooseImage({
      count: 12 - that.data.imgArray.length,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        for (let i = 0; i < res.tempFilePaths.length; i++) {
          that.data.imgArray.push(res.tempFilePaths[i])
        }
        that.setData({
          imgArray: that.data.imgArray
        })
        console.log(that.data.imgArray)
      }
    })
  },
  deleteImg: function (e) {
    let that = this
    let deleteIndex = e.currentTarget.dataset.index
    that.data.imgArray.splice(deleteIndex, 1)
    that.setData({
      imgArray: that.data.imgArray
    })
  },
  addBack: function (imgIdArray) {
    let that = this
    if(!that.data.reason){
      wx.showToast({
        title: '输入备注！',
        image:'/images/gantan.png'
      })
      return
    }
    let position = that.data.location || ''
    that.setData({
      imgIdArray
    })
    lbbService.updatePileStat({
        equId: parseInt(that.data.pileCode.replace(/[^0-9]/ig, "")),
        nowStat: 3,//维修中
        lbmid: parseInt(that.data.hospitalId),
        reason: that.data.reason,//更新原因
        imgs: that.data.imgIdArray,
        position,
      },function(res){
        wx.showToast({
          title: '已设置为维修',
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      })
  },
  formSubmit: function(e){
    let that = this
    let formData = e.detail.value
    that.setData({
      reason: formData.maintainReason
    })
    //上传图片 
    let imgArray = that.data.imgArray
    util.upLoadImg(app.globalData.header, imgArray, 0, [], that.addBack)
  }
})